package com.foreveross.bsl.plugin;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;

import android.app.Dialog;
import android.content.Intent;

import com.foreveross.bsl.CmanagerModule;
import com.foreveross.bsl.manager.CubeModuleManager;
import com.foreveross.bsl.model.CubeApplication;
import com.foreveross.bsl.util.BroadcastConstans;
import com.foreveross.bsl.util.Preferences;
import com.foreveross.chameleon.AppStatus;

/**
 * <BR>
 * [功能详细描述] 登出插件
 * 
 */
public class CubeLogoutPlugin extends CordovaPlugin {

	public Dialog dialog;

	@Override
	public boolean execute(String action, JSONArray args,
			final CallbackContext callbackContext) throws JSONException {
		if (action.equals("logout")) {
			logout(callbackContext);
		}
		return true;
	}

	private void logout(final CallbackContext callbackContext) {
		AppStatus.USERLOGIN = false;
		callbackContext.success("success");
		CubeApplication cubeApp = CubeApplication.getInstance(cordova.getActivity());

		String privileges = Preferences.getPrivileges(CmanagerModule.sharePref);
		CubeApplication remote_app = CubeApplication
				.buildApplication(privileges);
		CubeApplication comparedCubeApp = cubeApp.compareAndSetApp(cubeApp,
				remote_app);
		CubeModuleManager.getInstance().init(comparedCubeApp);

		cubeApp.save(cubeApp);
		cordova.getActivity().sendBroadcast(new Intent(BroadcastConstans.SecurityRefreshMainPage));
	}
}